package com.lyc.controller.user;

import com.lyc.pojo.Result;
import com.lyc.pojo.UserFollows;
import com.lyc.service.FollowService;
import com.lyc.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    /**
     * 关注用户
     */
    @PostMapping("/follow/{followingId}")
    public Result follow(@PathVariable Integer followingId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer currentUserId = (Integer) map.get("id");
        followService.follow(currentUserId, followingId);
        return Result.success("关注成功");
    }

    /**
     * 取消关注
     */
    @PostMapping("/unfollow/{followingId}")
    public Result unfollow(@PathVariable Integer followingId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer currentUserId = (Integer) map.get("id");
        followService.unfollow(currentUserId, followingId);
        return Result.success("取消关注成功");
    }

    /**
     * 获取用户关注列表
     */
    @GetMapping("/following/{userId}")
    public Result<List<UserFollows>> getFollowingList(@PathVariable Integer userId) {
        List<UserFollows> followingList = followService.getFollowingList(userId);
        return Result.success(followingList);
    }

    /**
     * 获取用户粉丝列表
     */
    @GetMapping("/followers/{userId}")
    public Result<List<UserFollows>> getFollowerList(@PathVariable Integer userId) {
        List<UserFollows> followerList = followService.getFollowerList(userId);
        return Result.success(followerList);
    }
}
